package ControlLayer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import DBLayer.DBBetaling;
import DBLayer.DBSaede;
import DBLayer.DBSalg;
import DBLayer.DBSalgSaede;
import DBLayer.DbConnection1;
import DBLayer.GetMax;
import ModelLayer.*;

public class SalgsCtr {
	
	private DateFormat dateFormat;
	private Date date;
	private int nextSalgId;
	
	public SalgsCtr()
	{
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		date = new Date();
	}
	/**
	 * Creates a new Salg with
	 * @param salgId
	 * @param dato
	 * @param billetAntal
	 * @param salgType
	 * @param f
	 * @param k
	 * @param s
	 * @param pris
	 */
	public void createSalg(int salgId, 
			String dato, byte billetAntal, 
			boolean salgType, Forestilling f, Kunde k, ArrayList<Saede> s, double pris) 
	{

		nextSalgId = GetMax.getMaxId("Select max (salgId) from Salg");
		nextSalgId = nextSalgId + 1;
		
		Salg salgObj = new Salg();
		
		salgObj.setSalgId(nextSalgId);
		salgObj.setDatoOprettet(dato);
		salgObj.setBilletAntal(billetAntal);
		salgObj.setSalgType(salgType);
		salgObj.setForestilling(f);
		salgObj.setKunde(k);
		salgObj.setSaede(s);
		
		//System.out.println(salgObj.getSalgId());
		try 
		{
			DbConnection1.startTransaction();
			DBSalg dbsalg = new DBSalg();
			dbsalg.insertSalg(salgObj);
			createBetaling(nextSalgId, pris, salgType);
			DbConnection1.commitTransaction();
		} 
		catch (Exception e) 
		{
			DbConnection1.rollbackTransaction();
		}
	}
	/**
	 * Creates a new Betaling with
	 * @param betalingId
	 * @param totalPris
	 * @param betalingsStatus
	 */
	public void createBetaling(int betalingId,  double totalPris, boolean betalingsStatus) 
	{

		Betaling betalObj = new Betaling();
		
		betalObj.setBetalId(betalingId);
		betalObj.setTotalPris(totalPris);
		betalObj.setBetalingsStatus(betalingsStatus);
		if(betalingsStatus == true)
		{
			betalObj.setBetalingsDato(dateFormat.format(date).toString());
		}
		else
		{
			betalObj.setBetalingsDato("0001-01-01");
		}
		
		//System.out.println(betalObj.getBetalId() + betalObj.getBetalingsDato() + betalObj.getTotalPris() + betalObj.getBetalingsStatus());
		try 
		{
			DbConnection1.startTransaction();
			DBBetaling dbbetal = new DBBetaling();
			dbbetal.insertBetaling(betalObj);
			DbConnection1.commitTransaction();
		} 
		catch (Exception e) 
		{
			DbConnection1.rollbackTransaction();
		}
	}
	/**
	 * Finds a Slag with
	 * @param salgId
	 * @return
	 */
	public Salg findSalg(int salgId) 
	{
		DBSalg dbsalg = new DBSalg();
		Salg result = dbsalg.getSalg(salgId, false);
		return result;
	}
	/**
	 * Finds a Betaling with
	 * @param salgId
	 * @return
	 */
	public Betaling findBetaling(int salgId)
	{
		DBBetaling dbBetaling = new DBBetaling();
		Betaling result = dbBetaling.getBetalingId(salgId, false);
		return result;
	}
	/**
	 * Removes a Salg with
	 * @param salgId
	 */
	public void removeSalg(int salgId) 
	{
		DBSalg dbsalg = new DBSalg();
		dbsalg.delete(salgId);
	}

	/**Updates a Salg with
	 * @param salgId
	 * @param billetAntal
	 * @param salgType
	 * @param f
	 * @param k
	 * @return
	 */
	public int updateSalg(int salgId, byte billetAntal, 
			boolean salgType, Forestilling f, Kunde k) 
	{
		
		DBSalg dbsalg = new DBSalg();
		Salg salgObj = findSalg(salgId);

		salgObj.setBilletAntal(billetAntal);
		salgObj.setSalgType(salgType);
		salgObj.setForestilling(f);
		salgObj.setKunde(k);
		
		return dbsalg.updateSalg(salgObj);
		
	}
	/**
	 * Finds a Saede with
	 * @param saedeId
	 * @return
	 */
	public Saede findSaede(int saedeId) 
	{
		//System.out.println(saedeId);
		DBSaede dbsaede = new DBSaede();
		Saede result = dbsaede.getSaede(saedeId, false);
		//System.out.println(result.getSaedeId());
		return result;
	}
	/** 
	 * Finds all Saeder from DBSaede
	 * @return
	 */
	public ArrayList<Saede> getAllSaede()
	{
		DBSaede dbsaede = new DBSaede();
		ArrayList<Saede> result = dbsaede.getAllSaede(false);
		return result;
	}
	/**
	 * Finds all Saeder from DBSalgSaede
	 * @return
	 */
	public ArrayList<SalgSaede> getAllSalgSaede()
	{
		DBSalgSaede dbSS = new DBSalgSaede();
		ArrayList<SalgSaede> result = dbSS.getAllSalgSaede(true);
		return result;
	}
	/**
	 * Finds all Betalinger
	 * @return
	 */
	public ArrayList<Betaling> getAllBetalinger()
	{
		DBBetaling dbB = new DBBetaling();
		ArrayList<Betaling> result = dbB.getAllBetalinger(true);
		return result;
	}
}
